﻿// https://leetcode-cn.com/problems/binary-tree-paths/
// 257. 二叉树的所有路径

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if (root == nullptr) return result;
        FindPath(root, path, result);
        return result;
    }

    void FindPath(TreeNode *root, vector<int> &path, vector<string> &result) {
        path.push_back(root->val);
        if (root->left == nullptr && root->right == nullptr) {
            string result_path;
            for (int i = 0; i < path.size() - 1; i++) {
                result_path += to_string(path[i]);
                result_path += "->";
            }
            result_path += to_string(path[path.size() - 1]);
            result.push_back(result_path);
            return;
        }

        if (root->left) {
            FindPath(root->left, path, result);
            path.pop_back();
        }
        if (root->right) {
            FindPath(root->right, path, result);
            path.pop_back();
        }
    }
};

int main() {
    Solution obj = Solution();
}